'use strict';

// 引入模块
let http = require('http');
let path = require('path');
let url = require('url');
let fs = require('fs');
//patch定位的当前文件夹的位置

let currentPath = path.resolve('.');
console.log(currentPath);

let server=http.createServer((req,res)=>{
    
    let reqPtch=req.url;
   console.log(reqPtch);

    if(reqPtch!=='/favicon.ico'){
            //  读取路径下所有的文件或文件夹
    let  PathName=path.join(currentPath,reqPtch);

    let files = fs.readdirSync(PathName);
    console.log(files);
   
  files.forEach(element => {
     
 if(element==='index.html'){
    //传入文件的绝对路境  写入组合目录文件夹和确文件
   let fullPath=path.join(currentPath,element);
   console.log(fullPath);
   fs.readFile(fullPath,(err,data)=>{
       if(!err){
           //成功的时候
          res.write(data)
          res.end();
       }else{
           //失败了
        res.writeHead(404);
        res.write('<head><meta charset="utf8"/></head>');
        res.end('好像有找到文件，但是显示不出来');
       }
   })
}else{
    console.log("没找到文件");
    console.log(element);
}
});
}
}
);
server.listen(3000);

console.log('当前服务运行在如下地址：http://127.0.0.1:3000');